home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1962 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1002 b   |  44 lines

  1. Path: sierra.net!usenet
  2. From: TGColwell <snowbull@sierra.net>
  3. Newsgroups: comp.lang.c++
  4. Subject: Returning ref to object - is this code invalid?
  5. Date: Sun, 14 Jan 1996 09:05:28 -0800
  6. Organization: Sierra-Net
  7. Message-ID: <30F937D8.12D2@sierra.net>
  8. NNTP-Posting-Host: 204.94.232.59
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0b5 (WinNT; I)
  13.  
  14. Does the following code create a dangling reference?
  15.  
  16. class vector
  17. { private: float x,y,z;
  18.   public:  //c'tors & functions
  19. };
  20.  
  21. class face
  22. {  private: vector v1, v2, v3;
  23.    public:
  24.         vector& get_vtx_1() const;
  25. }
  26.  
  27. vector& face::get_vtx_1() const  //returns reference to vector
  28. {  return v1;
  29. }
  30.  
  31. void main()
  32. {
  33.   face ff('constructor parameters);
  34.   vector vv = ff.get_vtx_1();
  35. }
  36.  
  37. I'm wondering if the return of a reference to v1 will invalidate
  38. my program since v1 goes out of scope when get_vtx_1() returns, 
  39. or does v1 stay in scope since ff is not deleted?
  40.  
  41. Any input is appreciated!
  42.  
  43. -TC
  44.